package com.itheima.exception;

import com.itheima.pojo.Result;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;


@RestControllerAdvice
public class GlobalException {
    @ExceptionHandler(Exception.class)
    public Result exception(Exception e){
        e.printStackTrace();
        return Result.error("出现异常,请联系管理员");
    }

    @ExceptionHandler(DuplicateKeyException.class)
    public Result exception(DuplicateKeyException e){
        e.printStackTrace();
        if (e.getMessage().contains("emp.username")){
            return Result.error("用户名已存在,请重新输入");
        }
        if (e.getMessage().contains("emp.phone")){
            return Result.error("手机号已存在,请重新输入");
        }
        return Result.error("数据已存在");
    }
    @ExceptionHandler(TliasException.class)
    public Result exception(TliasException e){
        e.printStackTrace();
        return Result.error(e.getMessage());
    }
}
